/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bs.ventas.dao;

import bs.global.dao.BaseDAO;
import bs.ventas.modelo.ItemListaPrecio;
import bs.ventas.modelo.ListaDePrecio;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.Query;


/**
 *
 * @author ctrosch
 */
@Stateless
public class ListaPrecioDAO extends BaseDAO{
    
    public List<ListaDePrecio> getLista(){
        
        try {

            String sQuery = "SELECT p FROM ListaDePrecio p ";
            sQuery += generarStringFiltro(filtro,"p", true);
            sQuery += " ORDER BY p.codigo";

//            System.err.println(sQuery);
            Query q = em.createQuery(sQuery);
            return q.getResultList();

        } catch (Exception e) {

            System.err.println("Error al consultar getItemsPrecioByFiltro");
            return new ArrayList<ListaDePrecio>();
        }
    }
    
    public List<ItemListaPrecio> getItemsPrecioByFiltro(Map<String, String> filtro) {
        try {

            String sQuery = "SELECT p FROM ItemListaPrecio p ";
            sQuery += generarStringFiltro(filtro,"p", true);
            sQuery += " ORDER BY p.artcod";

//            System.err.println(sQuery);
            Query q = em.createQuery(sQuery);            
            q.setMaxResults(20);
            
            return q.getResultList();

        } catch (Exception e) {

            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "getItemsPrecioByFiltro", e);  
            return new ArrayList<ItemListaPrecio>();
        }
    }
    
    public BigDecimal getPrecioByProducto(String codLis,String codigo) {
        try {

            String sQuery = "SELECT p.precio FROM ItemListaPrecio p "
                    + " WHERE p.auditoria.debaja = 'N' "
                    + " AND p.codlis = :codLis "
                    + " AND p.feclis <= :fechaVigencia "
                    + " AND p.producto.codigo =:codigo "                    
                    + " ORDER BY p.artcod";

            Query q = em.createQuery(sQuery);            

            q.setParameter("codLis", codLis);
            q.setParameter("fechaVigencia", new Date());            
            q.setParameter("codigo", codigo);            
            
            return (BigDecimal) q.getSingleResult();

        } catch (Exception e) {

            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "getPrecioByProducto", e);  
            return BigDecimal.ZERO;
        }
    }
    
    public ItemListaPrecio getItemsPrecioByProducto(String codLis,String codigo) {
        try {

            String sQuery = "SELECT p FROM ItemListaPrecio p "
                    + " WHERE p.auditoria.debaja = 'N' "
                    + " AND p.codlis = :codLis "
                    + " AND p.feclis <= :fechaVigencia "
                    + " AND p.producto.codigo =:codigo "                    
                    + " ORDER BY p.artcod";

            Query q = em.createQuery(sQuery);            

            q.setParameter("codLis", codLis);
            q.setParameter("fechaVigencia", new Date());            
            q.setParameter("codigo", codigo);            
            
            return (ItemListaPrecio) q.getSingleResult();

        } catch (Exception e) {

            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "getItemsPrecioByProducto", e);  
            return null;
        }
    }
    
}
